//
//  indexCollectionView.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/25.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "indexCollectionView.h"
#import "common.h"
#import "UIViewExt.h"
#import "messageModal.h"

@implementation indexCollectionView

- (id)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self _createCollectionViewCell ];
    }
    return self;
}

- (void)_createCollectionViewCell{
    self.delegate = self;
    self.dataSource = self;
    [self registerClass:[indexCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
}
- (void)setMovieModalArray:(NSArray *)movieModalArray{
    
    _movieModalArray = movieModalArray;
    [self reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _movieModalArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    indexCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    // cell.backgroundColor = [UIColor redColor];
    messageModal *modal = _movieModalArray[indexPath.row];
    cell.isBack = NO;
    cell.modal= modal;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(self.itemWidth, self.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
   
    return  UIEdgeInsetsMake(0, (Kwidth - self.itemWidth)/2 , 0, (Kwidth - self.itemWidth)/2 );

}


- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    
    CGFloat itemKidth =self.itemWidth;
    
    CGFloat x = targetContentOffset->x;
   
    NSInteger index = x/itemKidth;
    if (index < 0) {
        index = 0;
    }
    
    else if (index > _movieModalArray.count){
        index = _movieModalArray.count-1;
    }
    self.currentIndex = index;

    self.currentIndex = index;
    targetContentOffset->x = self.itemWidth*index;

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
     [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition: UICollectionViewScrollPositionCenteredHorizontally animated:YES];
      self.currentIndex = indexPath.row;//

}



@end
